package com.cyan.app.workflow.activity.SFC;

import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cyan.app.barcode.CaptureActivity;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseFragmentActivity;
import com.cyan.app.workflow.bean.BaseBean;
import com.cyan.app.workflow.bean.GdlkBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.ClickUtil;
import com.cyan.app.workflow.util.network.CommHttpListener;
import com.cyan.app.workflow.util.network.VolleyUtil;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by DXW on 2016/2/19.
 * 工单入库 - 入库
 */
public class GdlkLkActivity extends BaseFragmentActivity {
    public static final String TAG = "GdlkLkActivity";
    private TextView mTextViewTitle;
    private Button mButtonSubmit;
    private EditText mEditTextCk;
    private EditText mEditTextKw;
    private EditText mEditTextYg;
    private LinearLayout mBat;
    private GdlkBean mMannoLabBean;
    private Button mButtonKw;
    private Button mButtonYg;
    private static final String WAREH = "gdlk_lk_wareh";
    private static final String YGBH = "gdlk_lk_ygbh";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sfc_gdlk_lk);
        initView();

        mButtonKw =(Button)findViewById(R.id.sfc_gdlk_lk_ck_button);
        mButtonKw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(GdlkLkActivity.this, GdlkCkActivity.class);
                startActivityForResult(intent, 0x0001);
            }
        });
        mButtonKw =(Button)findViewById(R.id.sfc_gdlk_lk_kw_sm);
        mButtonKw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(GdlkLkActivity.this, CaptureActivity.class), 0x0002);
            }
        });
        mButtonYg =(Button)findViewById(R.id.sfc_gdlk_lk_ygk_sm);
        mButtonYg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(GdlkLkActivity.this, CaptureActivity.class), 0x0003);
            }
        });

        mBat = (LinearLayout)findViewById(R.id.sfc_gdlk_lk_bat);
        mBat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("GdlzLkItnbr", mMannoLabBean.getMannos().get(0).getItnbr());
                intent.setClass(GdlkLkActivity.this,ItnbrBatActivity.class);
                startActivity(intent);
            }
        });
        mButtonSubmit = (Button)findViewById(R.id.sfc_gdlk_lk_submit);
        mButtonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(ClickUtil.isFastClick(20)){
                    //20秒内不能重复操作
                    return;
                }
                if (mEditTextCk.getText().toString().isEmpty()) {
                    AppUtils.showTip(GdlkLkActivity.this, "仓库不能为空");
                }else if (mEditTextKw.getText().toString().isEmpty()) {
                    AppUtils.showTip(GdlkLkActivity.this, "库位不能为空");
                }else if (mEditTextYg.getText().toString().isEmpty()) {
                    AppUtils.showTip(GdlkLkActivity.this, "员工不能为空");
                } else if (mMannoLabBean==null||mMannoLabBean.getMannos().size()==0) {
                    AppUtils.showTip(GdlkLkActivity.this, "没有可入库的工单");
                }else {
                    execLk();
                }
            }
        });
    }
    //执行流转
    private void execLk(){
        String result = null;
        try {
            Map<String, String> params = new HashMap<String, String>();
            JSONArray array = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            for(GdlkBean.GdlkMannoBean c:mMannoLabBean.getMannos()){
                if(c.isSelected()) {
                    array.put(c.toJSON());
                }
            }
            jsonObject.put("Keyinuserno",mEditTextYg.getText());
            jsonObject.put("Wareh", mEditTextCk.getText());
            jsonObject.put("Fixnr", mEditTextKw.getText());
            jsonObject.put("MannoInfo",array.toString());
            params.put("action", "206");
            params.put("params", jsonObject.toString());
            VolleyUtil.sendStringRequestByGet(GdlkLkActivity.this, HttpConfig.HTTP_MAIN_SERVER_URL, TAG, params, null, new CommHttpListener<Object>() {
                @Override
                public void onResponse(String result) {
                    super.onResponse(result);
                    if (result == null) {
                        return;
                    }
                    BaseBean resultBean = new Gson().fromJson(result, BaseBean.class);
                    if (resultBean.getCode() == 1) {
                        AppUtils.showTip(GdlkLkActivity.this, "入库成功");
                        for(int i= mMannoLabBean.getMannos().size()-1;i>=0;i--){
                            if(mMannoLabBean.getMannos().get(i).isSelected()) {
                                mMannoLabBean.deleteCrime(mMannoLabBean.getMannos().get(i));
                            }
                        }
                        setResult(RESULT_OK);
                    } else {
                        AppUtils.showTip(GdlkLkActivity.this, resultBean.getMsg());
                    }
                    finish();
                }

                @Override
                public void onCodeError(int code, String msg) {
                    AppUtils.showTip(GdlkLkActivity.this, msg);
                }

                @Override
                public void onVolleyError(VolleyError volleyError) {
                    AppUtils.showTip(GdlkLkActivity.this, R.string.network_error);
                }

                @Override
                public void onException(Exception e) {
                    AppUtils.showTip(GdlkLkActivity.this, R.string.network_error);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            AppUtils.showTip(GdlkLkActivity.this, "执行失败，请联系IT人员");
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x0002 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("30010")) {
                AppUtils.showTip(GdlkLkActivity.this, "库位不存在");
            }else{
                mEditTextKw.setText(String.valueOf(result.substring(5)));
            }
        }else if(requestCode == 0x0003 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("40010")) {
                AppUtils.showTip(GdlkLkActivity.this, "员工不存在");
            }else {
                mEditTextYg.setText(String.valueOf(result.substring(5)));
                PreferenceManager.getDefaultSharedPreferences(this).edit().putString(YGBH,result.substring(5)).commit();
            }
        }else if(requestCode == 0x0001 && resultCode == RESULT_OK){
            String result = "";
            if(data != null) {
                result = data.getStringExtra("wareh");
                mEditTextCk.setText(String.valueOf(result));
                PreferenceManager.getDefaultSharedPreferences(this).edit().putString(WAREH,result).commit();
            }
        }
    }
    private void initView(){
        titleBar();
        mEditTextCk= (EditText)findViewById(R.id.sfc_gdlk_lk_ck_edit);
        mEditTextCk.setInputType(InputType.TYPE_NULL);
        String wareh = PreferenceManager.getDefaultSharedPreferences(this).getString(WAREH,null);
        if(wareh!=null){
            mEditTextCk.setText(String.valueOf(wareh));
        }
    //库位禁止键盘输入
    mEditTextKw = (EditText)findViewById(R.id.sfc_gdlk_lk_kw);
    mEditTextKw.setInputType(InputType.TYPE_NULL);
    //员工禁止键盘输入
    mEditTextYg = (EditText)findViewById(R.id.sfc_gdlk_lk_ygk);
    mEditTextYg.setInputType(InputType.TYPE_NULL);
        String ygbh = PreferenceManager.getDefaultSharedPreferences(this).getString(YGBH,null);
        if(ygbh!=null){
            mEditTextYg.setText(String.valueOf(ygbh));
        }
    mMannoLabBean = GdlkBean.get(this);
}
    private void titleBar(){
        mTextViewTitle =(TextView)findViewById(R.id.top_title_info);
        mTextViewTitle.setText("工单入库提交");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }
}
